home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWMemory / Include / FWObjecH.h < prev    next >
Encoding:
Text File  |  1994-04-21  |  13.0 KB  |  422 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWObjecH.h
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWOBJECH_H
  13. #define FWOBJECH_H
  14.  
  15. #ifndef FWPLATME_H
  16. #include <FWPlatMe.h>
  17. #endif
  18.  
  19. #ifndef FWMEMORH_H
  20. #include <FWMemorH.h>
  21. #endif
  22.  
  23. #ifndef FWBESTFH_H
  24. #include <FWBestFH.h>
  25. #endif
  26.  
  27.  
  28. //========================================================================================
  29. // Forward class declarations
  30. //========================================================================================
  31.  
  32. class FW_CChunk;
  33. class FW_CPrivChunkyBlock;
  34. class FW_CObjectHeap;
  35. class FW_CPrivChunkyBlockStack;
  36.  
  37.  
  38. //========================================================================================
  39. // CLASS FW_CPrivChunkyBlock
  40. //========================================================================================
  41.  
  42. #ifdef BUILD_WIN
  43. // Bytes are in reverse order in a word.
  44.  
  45. const unsigned short ChunkyBlock_kSizeIndexMask = 0x00F0;
  46. const unsigned short ChunkyBlock_kSizeIndexShift = 4;
  47.  
  48. const unsigned short ChunkyBlock_kBlockIndexMask = 0x000F;
  49. const unsigned short ChunkyBlock_kBlockIndexShift = 0;
  50.  
  51. const unsigned short ChunkyBlock_kBlockTypeMask = 0xF000;
  52. const unsigned short ChunkyBlock_kBlockTypeShift = 12;
  53.  
  54. const unsigned short ChunkyBlock_kMagicNumberMask = 0x0F00;
  55. const unsigned short ChunkyBlock_kMagicNumberShift = 8;
  56. #else
  57. const unsigned short ChunkyBlock_kSizeIndexMask = 0xF000;
  58. const unsigned short ChunkyBlock_kSizeIndexShift = 12;
  59.  
  60. const unsigned short ChunkyBlock_kBlockIndexMask = 0x0F00;
  61. const unsigned short ChunkyBlock_kBlockIndexShift = 8;
  62.  
  63. const unsigned short ChunkyBlock_kBlockTypeMask = 0x00F0;
  64. const unsigned short ChunkyBlock_kBlockTypeShift = 4;
  65.  
  66. const unsigned short ChunkyBlock_kMagicNumberMask = 0x000F;
  67. const unsigned short ChunkyBlock_kMagicNumberShift = 0;
  68. #endif
  69.  
  70. class FW_CPrivChunkyBlock
  71. {
  72. public:
  73.     enum
  74.     {
  75.         kBusyOverhead = sizeof(unsigned short), 
  76.         kBlockTypeId = FW_CPrivBestFitBlock::kBlockTypeId + 1, 
  77.         kMagicNumber = 0xA
  78.     };
  79.  
  80.     void *operator new(SIZE_T, void *ptr);
  81.     void *operator new(SIZE_T);
  82.     void operator delete(void *) { };
  83.  
  84.     FW_CPrivChunkyBlock();
  85.     FW_CPrivChunkyBlock(unsigned int sizeIndex, unsigned int blockIndex);
  86.     FW_CPrivChunkyBlock(const FW_CPrivChunkyBlock& blk);
  87.     FW_CPrivChunkyBlock& operator=(const FW_CPrivChunkyBlock& blk);
  88.     
  89.     unsigned short GetSizeIndex() const;
  90.     void SetSizeIndex(unsigned short index);
  91.     
  92.     unsigned short GetBlockIndex() const;
  93.     void SetBlockIndex(unsigned short index);
  94.     
  95.     unsigned short GetBlockType() const;
  96.     void SetBlockType(unsigned short type);
  97.     
  98.     unsigned short GetMagicNumber() const;
  99.     void SetMagicNumber(unsigned short magic);    
  100.  
  101.     FW_CChunk* GetChunk(FW_BlockSize blkSize);
  102.     
  103.     FW_CPrivChunkyBlock* GetNext();
  104.     void SetNext(FW_CPrivChunkyBlock* blk);
  105.  
  106.     Boolean IsBusy(FW_BlockSize blockSize);
  107.     void SetBusy(FW_BlockSize blockSize, Boolean busy);
  108.  
  109. private:
  110.     // Fields present in both free and busy blocks. Several bit fields are stored in
  111.     // the following fields. They are accessed using get and set methods.
  112.  
  113.     unsigned short fBits;
  114.  
  115.     // Fields present in only free blocks.
  116.  
  117.     FW_CPrivChunkyBlock* fNext;
  118. };
  119.  
  120.  
  121. //========================================================================================
  122. // CLASS FW_CPrivChunkyBlockStack
  123. //========================================================================================
  124.  
  125. class FW_CPrivChunkyBlockStack
  126. {
  127. public:
  128.     FW_CPrivChunkyBlockStack();
  129.     FW_CPrivChunkyBlockStack(const FW_CPrivChunkyBlockStack& blk);
  130.     ~FW_CPrivChunkyBlockStack();
  131.     FW_CPrivChunkyBlockStack& operator=(const FW_CPrivChunkyBlockStack& blk);
  132.  
  133.     FW_CPrivChunkyBlock* Pop();
  134.     void Push(FW_CPrivChunkyBlock* blk);
  135.     void RemoveRange(void *begAddr, void *endAddr);
  136.     FW_CPrivChunkyBlock* Top();
  137.  
  138. private:
  139.     FW_CPrivChunkyBlock fHead;
  140. };
  141.  
  142.  
  143. //========================================================================================
  144. // CLASS FW_CChunk
  145. //========================================================================================
  146.  
  147. struct FW_SPrivChunkHeader
  148. {
  149.     unsigned short fBlockBusyBits;
  150. };
  151.  
  152. class FW_CChunk
  153. {
  154. public:
  155.     FW_CChunk(short blocksPerChunk,
  156.               unsigned int sizeIndex,
  157.               FW_BlockSize blockSize);
  158.  
  159.     void *operator new(SIZE_T, void* ptr);
  160.     void *operator new(SIZE_T);
  161.     void operator delete(void *) { };
  162.  
  163.     FW_CPrivChunkyBlock* GetBlock(unsigned int blkIndex, FW_BlockSize blkSize);
  164.     unsigned int GetSizeIndex();
  165.  
  166.     Boolean IsBlockBusy(unsigned int whichBlock);
  167.     Boolean IsBusy();
  168.  
  169.     void SetBlockBusy(unsigned int whichBlock, Boolean busy);
  170.  
  171. private:
  172.     FW_SPrivChunkHeader fHeader;
  173.     
  174.     FW_CChunk(const FW_CChunk& blk);
  175.     FW_CChunk& operator=(const FW_CChunk& blk);
  176.         // This class shouldn't be copied.
  177. };
  178.  
  179.  
  180. //========================================================================================
  181. // CLASS FW_CObjectHeap
  182. //========================================================================================
  183.  
  184. const FW_BlockSize ObjectHeap_kDefaultBlockSizes[] = {sizeof(FW_CPrivChunkyBlock), 10, 14, 18, 0};
  185.  
  186. class FW_CObjectHeap : public FW_CBestFitHeap
  187. {
  188. private:
  189.  
  190. public:
  191.     enum
  192.     {
  193.         kMaxNumberOfBlockSizes = 16,
  194.         kDefaultBlocksPerChunk = 4,
  195.         kDefaultInitialSize = 10240,
  196.         kDefaultIncrementSize = 4096
  197.     };
  198.  
  199.     FW_CObjectHeap(unsigned long initialSize,
  200.              unsigned long incrementSize = 0,
  201.              short blocksPerChunk = kDefaultBlocksPerChunk);
  202.  
  203.     FW_CObjectHeap(const FW_BlockSize* blockSizes = ObjectHeap_kDefaultBlockSizes,
  204.              unsigned long initialSize = kDefaultInitialSize,
  205.              unsigned long incrementSize = kDefaultIncrementSize,
  206.              short blocksPerChunk = kDefaultBlocksPerChunk);
  207.  
  208.     void IObjectHeap();
  209.  
  210.     virtual ~FW_CObjectHeap();
  211.  
  212. protected:
  213.     virtual void* DoAllocate(FW_BlockSize size, FW_BlockSize& allocatedSize);
  214.     virtual FW_BlockSize DoBlockSize(const void* block) const;
  215.     virtual void DoFree(void*);
  216.     virtual void DoReset();
  217.  
  218.     void* AllocateBlock(unsigned int sizeIndex);
  219.     void CreateNewChunk(unsigned int sizeIndex);
  220.     void FreeBlock(FW_CPrivChunkyBlock* blk);
  221.     unsigned int SizeIndex(FW_BlockSize size);
  222.  
  223. #ifdef FW_DEBUG
  224.     virtual void CompilerCheck();
  225.     virtual Boolean DoIsValidBlock(void* blk) const;
  226. #endif
  227.  
  228. private:
  229.     
  230.     short fNumberOfBlockSizes;
  231.     const FW_BlockSize* fBlockSizes;
  232.     FW_CPrivChunkyBlockStack fFreeLists[kMaxNumberOfBlockSizes];
  233.     short fBlocksPerChunk;
  234.     
  235.     FW_CObjectHeap(const FW_CObjectHeap& blk);
  236.     FW_CObjectHeap& operator=(const FW_CObjectHeap& blk);
  237.         // This class shouldn't be copied.
  238. };
  239.  
  240.  
  241. //========================================================================================
  242. // CLASS FW_CPrivChunkyBlock
  243. //========================================================================================
  244.  
  245. //----------------------------------------------------------------------------------------
  246. // FW_CPrivChunkyBlock::operator new
  247. //----------------------------------------------------------------------------------------
  248.  
  249. inline void *FW_CPrivChunkyBlock::operator new(SIZE_T, void *ptr)
  250. {
  251.     return ptr;
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. // FW_CPrivChunkyBlock::operator new
  256. //----------------------------------------------------------------------------------------
  257.  
  258. inline void *FW_CPrivChunkyBlock::operator new(SIZE_T)
  259. {
  260.     return NULL;
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // FW_CPrivChunkyBlock::FW_CPrivChunkyBlock
  265. //----------------------------------------------------------------------------------------
  266.  
  267. inline FW_CPrivChunkyBlock::FW_CPrivChunkyBlock(const FW_CPrivChunkyBlock& blk) :
  268.     fBits(blk.fBits),
  269.     fNext(blk.fNext)
  270. {
  271. }
  272.  
  273. //----------------------------------------------------------------------------------------
  274. // FW_CPrivChunkyBlock::operator=
  275. //----------------------------------------------------------------------------------------
  276.  
  277. inline FW_CPrivChunkyBlock& FW_CPrivChunkyBlock::operator=(const FW_CPrivChunkyBlock& blk)
  278. {
  279.     fBits = blk.fBits;
  280.     fNext = blk.fNext;
  281.     return *this;
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // FW_CPrivChunkyBlock::GetSizeIndex
  286. //----------------------------------------------------------------------------------------
  287.  
  288. inline unsigned short FW_CPrivChunkyBlock::GetSizeIndex() const
  289. {
  290.     return (fBits & ChunkyBlock_kSizeIndexMask) >> ChunkyBlock_kSizeIndexShift;
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. // FW_CPrivChunkyBlock::SetSizeIndex
  295. //----------------------------------------------------------------------------------------
  296.  
  297. inline void FW_CPrivChunkyBlock::SetSizeIndex(unsigned short index)
  298. {
  299.     fBits &= ~ChunkyBlock_kSizeIndexMask;
  300.     fBits |= (index << ChunkyBlock_kSizeIndexShift) & ChunkyBlock_kSizeIndexMask;
  301. }
  302.     
  303. //----------------------------------------------------------------------------------------
  304. // FW_CPrivChunkyBlock::GetBlockIndex
  305. //----------------------------------------------------------------------------------------
  306.  
  307. inline unsigned short FW_CPrivChunkyBlock::GetBlockIndex() const
  308. {
  309.     return (fBits & ChunkyBlock_kBlockIndexMask) >> ChunkyBlock_kBlockIndexShift;
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. // FW_CPrivChunkyBlock::SetBlockIndex
  314. //----------------------------------------------------------------------------------------
  315.  
  316. inline void FW_CPrivChunkyBlock::SetBlockIndex(unsigned short index)
  317. {
  318.     fBits &= ~ChunkyBlock_kBlockIndexMask;
  319.     fBits |= (index << ChunkyBlock_kBlockIndexShift) & ChunkyBlock_kBlockIndexMask;
  320. }
  321.     
  322. //----------------------------------------------------------------------------------------
  323. // FW_CPrivChunkyBlock::GetBlockType
  324. //----------------------------------------------------------------------------------------
  325.  
  326. inline unsigned short FW_CPrivChunkyBlock::GetBlockType() const
  327. {
  328.     return (fBits & ChunkyBlock_kBlockTypeMask) >> ChunkyBlock_kBlockTypeShift;
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------
  332. // FW_CPrivChunkyBlock::SetBlockType
  333. //----------------------------------------------------------------------------------------
  334.  
  335. inline void FW_CPrivChunkyBlock::SetBlockType(unsigned short type)
  336. {
  337.     fBits &= ~ChunkyBlock_kBlockTypeMask;
  338.     fBits |= (type << ChunkyBlock_kBlockTypeShift) & ChunkyBlock_kBlockTypeMask;
  339. }
  340.     
  341. //----------------------------------------------------------------------------------------
  342. // FW_CPrivChunkyBlock::GetMagicNumber
  343. //----------------------------------------------------------------------------------------
  344.  
  345. inline unsigned short FW_CPrivChunkyBlock::GetMagicNumber() const
  346. {
  347.     return (fBits & ChunkyBlock_kMagicNumberMask) >> ChunkyBlock_kMagicNumberShift;
  348. }
  349.  
  350. //----------------------------------------------------------------------------------------
  351. // FW_CPrivChunkyBlock::SetMagicNumber
  352. //----------------------------------------------------------------------------------------
  353.  
  354. inline void FW_CPrivChunkyBlock::SetMagicNumber(unsigned short magic)
  355. {
  356.     fBits &= ~ChunkyBlock_kMagicNumberMask;
  357.     fBits |= (magic << ChunkyBlock_kMagicNumberShift) & ChunkyBlock_kMagicNumberMask;
  358. }    
  359.  
  360. //----------------------------------------------------------------------------------------
  361. // FW_CPrivChunkyBlock::GetNext
  362. //----------------------------------------------------------------------------------------
  363.  
  364. inline FW_CPrivChunkyBlock* FW_CPrivChunkyBlock::GetNext()
  365. {
  366.     return fNext;
  367. }
  368.  
  369. //----------------------------------------------------------------------------------------
  370. // FW_CPrivChunkyBlock::SetNext
  371. //----------------------------------------------------------------------------------------
  372.  
  373. inline void FW_CPrivChunkyBlock::SetNext(FW_CPrivChunkyBlock* blk)
  374. {
  375.     fNext = blk;
  376. }
  377.  
  378.  
  379. //========================================================================================
  380. // CLASS FW_CChunk
  381. //========================================================================================
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // FW_CChunk::operator new
  385. //----------------------------------------------------------------------------------------
  386.  
  387. inline void* FW_CChunk::operator new(SIZE_T, void *ptr)
  388. {
  389.     return ptr;
  390. }
  391.  
  392. //----------------------------------------------------------------------------------------
  393. // FW_CChunk::operator new
  394. //----------------------------------------------------------------------------------------
  395.  
  396. inline void* FW_CChunk::operator new(SIZE_T)
  397. {
  398.     return NULL;
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. // FW_CChunk::GetSizeIndex
  403. //----------------------------------------------------------------------------------------
  404.  
  405. inline unsigned int FW_CChunk::GetSizeIndex()
  406. {
  407.     FW_CPrivChunkyBlock *block 
  408.         = (FW_CPrivChunkyBlock *) ((FW_BytePtr) this + sizeof(FW_SPrivChunkHeader));
  409.     return block->GetSizeIndex();
  410. }
  411.  
  412. //----------------------------------------------------------------------------------------
  413. // FW_CChunk::IsBusy
  414. //----------------------------------------------------------------------------------------
  415.  
  416. inline Boolean FW_CChunk::IsBusy()
  417. {
  418.     return fHeader.fBlockBusyBits != 0;
  419. }
  420.  
  421. #endif
  422.